package com.bt.lms.service.impl;

import com.bt.lms.constant.BOConstant;
import com.bt.lms.entity.Major;
import com.bt.lms.entity.StudyCen;
import com.bt.lms.entity.page.Page;
import com.bt.lms.dao.servise.StudyCenDao;
import com.bt.lms.dao.servise.impl.StudyCenDaoImpl;
import com.bt.lms.service.StudyCenService;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 学习中心服务
 *
 * @author 徐大永
 */
public class StudyCenServiceImpl implements StudyCenService {

    @Override
    public Page<StudyCen> pagingQuery(Page<StudyCen> page) {

        //查询本表信息  sql（具体数据）
        StringBuilder sqlSB = new StringBuilder();
        sqlSB.append("SELECT * FROM study_centre WHERE deleted=").append(BOConstant.DELETED_FALSE).append(" LIMIT ?,?");
        String sql = new String(sqlSB);
        //sql 数据总量
        StringBuilder sqlSumSB = new StringBuilder();
        sqlSumSB.append("SELECT COUNT(*) FROM study_centre WHERE deleted=").append(BOConstant.DELETED_FALSE);
        String sqlSum = new String(sqlSumSB);

        //dao 方法
        StudyCenDao studyCenDao = new StudyCenDaoImpl();
        studyCenDao.select(sql, page, sqlSum);

        return page;

    }

    @Override
    public StudyCen detail(Long id) {

        //要返回的学习中心
        StudyCen studyCen = new StudyCen();
        //关系的专业
        List<Major> majorInfos = new ArrayList<>();
        studyCen.setMajorInfos(majorInfos);
        studyCen.setId(id);

        //dao对象
        StudyCenDao studyCenDao = new StudyCenDaoImpl();

        //学习中心本表
        //1.SQL
        String studyCenSql = "SELECT * FROM study_centre WHERE id = ?";
        //2.dao 得，含有内容的学习中心
        studyCenDao.select(studyCenSql, studyCen);

        //学习中心，专业关系表
        //1.SQL
        String rlSql = "SELECT major_id FROM rl_major_study_centre WHERE study_centre_id = ?";
        //2.dao 得，专业id
        List<Long> majors = studyCenDao.selectRl(rlSql, id);

        for (Long majorId : majors) {
            //专业表
            //1.SQL
            String majorSql = "SELECT name FROM major WHERE id = ?";
            //2.dao 得 专业
            Major major = studyCenDao.select(majorSql, majorId);
            majorInfos.add(major);
        }
        return studyCen;
    }

    @Override
    public boolean add(StudyCen studyCen) {

        //关于表，和几张表有关系 study_centre 本表
        //1.需要系统维护那些属性
        UUID uuid = UUID.randomUUID();
        String codeStr = uuid.toString();
        studyCen.setCode(codeStr);
        //2.准备动库
        //2.1 SQL
        String sql = "INSERT INTO study_centre (name,code,relation,phone,email,address,describe_) VALUES (?,?,?,?,?,?,?)";
        //2.2 调用dao
        StudyCenDao studyCenDao = new StudyCenDaoImpl();
        int i = studyCenDao.insert(sql, studyCen);

        return i == 1;

    }

    @Override
    public boolean compile(StudyCen studyCen) {

        //sql
        String sql = "UPDATE study_centre SET name=?,code=?,relation=?,phone=?,email=?,describe_=?,address=? WHERE id=?";

        //dao方法
        StudyCenDao studyCenDao = new StudyCenDaoImpl();
        int i = studyCenDao.update(sql, studyCen);

        return i == 1;

    }

    @Override
    public boolean del(Long id) {

        //dao对象
        StudyCenDao studyCenDao = new StudyCenDaoImpl();

        //sql（本表）
        StringBuilder stuCenSqlSB = new StringBuilder();
        stuCenSqlSB.append("UPDATE study_centre SET deleted=").append(BOConstant.DELETED_TRUE).append(" WHERE id = ?");
        String stuCenSql = new String(stuCenSqlSB);
        int i = studyCenDao.update(stuCenSql, id);
        if (i == 1) {
            //sql（关系表）
            StringBuilder rlSqlSB = new StringBuilder();
            rlSqlSB.append("UPDATE rl_major_study_centre SET deleted=").append(BOConstant.DELETED_TRUE)
                    .append(" WHERE study_centre_id = ?");
            String rlSql = new String(rlSqlSB);
            studyCenDao.update(rlSql, id);
        } else
            return false;

        return true;
    }

}
